import { getPackageList, queryProjectByPackageId } from '../../services/inspect-project'
const app = getApp()

Page({
  data: {
    currentId: 0,
    currentItem: {},
    packageList:[],
    packageDetailList:[],
    showDialogAlert: false,
    dialogMsg: ""
  },

  onLoad(options){
    if(!options){
      return
    }
    const { id } = options
    if(id){
      this.setData({
        currentId: id
      })
  
      this.handleQueryProjectByPid(id)
    }

    const globalInfo = app.globalData.globalInfo
    this.queryPackageList({
      addr: globalInfo.stationId
    })
  },

  async queryPackageList(data){
    let res = await getPackageList(data)
    if(res.code === 200){
      let packageList = res.rows
      let currentItem = {}
      if(this.data.currentId){
        currentItem = packageList.find(item => item.id === this.data.currentId)
      }
       
      this.setData({
        packageList,
        currentItem
      })
    }
  },

  async handleQueryProjectByPid(Id){
    let projectRes = await queryProjectByPackageId(Id)
    let packageTempList = []
    if(projectRes.code === 200){
      packageTempList = projectRes.rows
    }
    this.setData({
      packageDetailList: packageTempList
    })
  },

  async handleClick(e){
    const { currentid, currentitem } = e.currentTarget.dataset
    console.log("currentid, currentitem>>", currentid, currentitem)
    this.handleQueryProjectByPid(currentid)

    this.setData({
      currentId: currentid,
      currentItem: currentitem,
    })
  },

  confirmInfo(){
    const { id, name, detectProjectName } = this.data.currentItem
    console.log("currentItem>>>", this.data.currentItem)
    if(!id){
      this.setData({
        showDialogAlert:true,
        dialogMsg:"请先选择送检项目!"
      })
      return
    }

    app.globalData.globalInfo.appointPackageId = id
    app.globalData.globalInfo.appointPackageName = name
    app.globalData.globalInfo.detectProjectName = detectProjectName


    wx.navigateBack()
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit("infoChange", this.data.currentItem)
  },


})